//jqGrid的配置信息
if($.jgrid){
    $.jgrid.defaults.width = 1000;
    $.jgrid.defaults.responsive = true;
    $.jgrid.defaults.styleUI = 'Bootstrap';
}
var pwdRegex = new RegExp('(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}');
var baseURL = "../../";

//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;
var xsrfToken = $.cookie('XSRF-TOKEN');
//全局配置
var globalAjaxLodingIndex;
var globalAjaxLodingFlag = true;
$.ajaxSetup({
	dataType: "json",
	cache: false,
    headers: {
        'X-XSRF-TOKEN':xsrfToken
    },
    beforeSend: function () {
	    if(globalAjaxLodingFlag){
            globalAjaxLodingIndex = loading("加载中...");
        }
    },
	error: function(resp, textStatus){
		if(globalAjaxLodingFlag){
            closeLoading(globalAjaxLodingIndex);
        }
        if(resp.status == 401){
            parent.location.reload();
        }
        if(resp.status == 302){
            parent.location.reload();
        }
	},
    complete: function (resp) {
	    if(globalAjaxLodingFlag){
            closeLoading(globalAjaxLodingIndex);
        }
        try{
            var data = resp.responseJSON;
            if(data.code == -4){
                layer.msg("会话超时请刷新或者重新登录!!!", {icon: 2, time:8000});
			}
			if(resp.status == 401){
                parent.location.reload();
			}
			if(resp.status == 302){
                parent.location.reload();
			}
		}catch (e) {

        }
    }
});
function loading(msg) {
    return layer.msg(msg?msg:"加载中...", { icon: 16, shade: 0.1,shadeClose:false,time:30000 });
}
function closeLoading(flag){
    layer.close(flag);
}
//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

function setCookie(name, value, day) {
    // $.cookie(name, value, { expires: day, secure: true });
    $.cookie(name, value, { expires: day});
}
function getCookie(name)
{
    var cookie = $.cookie(name);
    if(cookie)
        return cookie;
    else
        return null;
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0] ? selectedIDs[0] : rowKey;
}

//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    return grid.getGridParam("selarrrow");
}
function getSelectedRowData() {
    var rowid = getSelectedRow();
    var jqGrid = $("#jqGrid");
    var rowData = jqGrid.jqGrid("getRowData",rowid);
    // $.each(rowData, function(index, item){
    // 	var index = jqGrid.getColProp(item).;
    //
    // });
    return rowData;
}
//判断是否为空
function isBlank(value) {
    return !value || !/\S/.test(value)
}
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");

    var r = window.location.search.substr(1).match(reg);

    if (r != null) return unescape(r[2]); return null;
}